package com.sgb.kjwl.viewmodel.manage;

import android.content.Context;
import android.util.Log;

import androidx.lifecycle.MutableLiveData;

import com.blankj.utilcode.util.RegexUtils;
import com.sgb.kjwl.api.APIManager;
import com.sgb.kjwl.view.ui.activity.manage.WebsiteUpDataZhuanXiangZiZhiActivity;
import com.swgk.core.base.api.ICallBack;
import com.swgk.core.util.PickSelectEntity;
import com.sgb.kjwl.model.entity.manage.UpdataSpecialQualificationEntity;
import com.swgk.core.base.BaseViewModel;
import com.swgk.core.base.model.entity.BaseEntity;
import com.swgk.core.dialog.DialogHelper;
import com.swgk.core.util.MToast;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import okhttp3.MediaType;
import okhttp3.RequestBody;

/**
 * @author 张驰
 * @date 2021/5/18 19:23
 * @Description 专项资质model
 * @name com.sgb.kjwl.viewmodel.manage
 */
public class WebsiteUpDataZhuanXiangZiZhiModel extends BaseViewModel {

    //专项资质详情
    private MutableLiveData<UpdataSpecialQualificationEntity> specialQualificationEntityLiveData = new MutableLiveData<>(new UpdataSpecialQualificationEntity());
    //专项资质类型
    private MutableLiveData<List<PickSelectEntity<List<PickSelectEntity>>>> typePickSelectList = new MutableLiveData<>(new ArrayList<>());
    //专项资质行业类型
    private MutableLiveData<List<PickSelectEntity<List<PickSelectEntity>>>> typehangyeSelectList = new MutableLiveData<>(new ArrayList<>());
    //资质等级
    private MutableLiveData<List<PickSelectEntity>> nomberPickSelectList = new MutableLiveData<>(new ArrayList<>());
    //展示权限
    private MutableLiveData<List<PickSelectEntity>> permissionPickSelectList = new MutableLiveData<>(new ArrayList<>());
    //备案单位
    private MutableLiveData<List<PickSelectEntity<List<PickSelectEntity>>>> findProvinceRecordUnit = new MutableLiveData<>(new ArrayList<>());


    private MutableLiveData<String> urlString = new MutableLiveData<>("");

    public MutableLiveData<UpdataSpecialQualificationEntity> getMutableLiveData() {
        return specialQualificationEntityLiveData;
    }

    public MutableLiveData<List<PickSelectEntity<List<PickSelectEntity>>>> getTypePickSelectList() {
        return typePickSelectList;
    }
    public MutableLiveData<List<PickSelectEntity<List<PickSelectEntity>>>> getTypeHangyeSelectList() {
        return typehangyeSelectList;
    }

    public MutableLiveData<List<PickSelectEntity>> getPermissionPickSelectList() {
        return permissionPickSelectList;
    }

    public MutableLiveData<String> getUrlString() {
        return urlString;
    }

    public MutableLiveData<List<PickSelectEntity<List<PickSelectEntity>>>> getFieldPickSelectList() {
        return findProvinceRecordUnit;
    }

    public MutableLiveData<List<PickSelectEntity>> getNumberPickSelectList() {
        return nomberPickSelectList;
    }

    /**
     * 专项资质详情
     * @param mActivity
     * @param companyNo
     */
    public void getSpecialQualificationDetail(WebsiteUpDataZhuanXiangZiZhiActivity mActivity, String companyNo) {
        DialogHelper.showProgressDialog(mActivity, null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
        APIManager.getInstance().getWebsiteInfoAPI().getSpecialQualificationDetail(companyNo).enqueue(new ICallBack<BaseEntity<UpdataSpecialQualificationEntity>>() {
            @Override
            public void onSuccess(BaseEntity<UpdataSpecialQualificationEntity> data) {
                if (data.getState().equals(APIManager.RESPONSE_OK)) {
                    specialQualificationEntityLiveData.postValue(data.getData());
                    mActivity.setview(data.getData());
                }
            }
        });
    }

    /**
     * 专项资质 新增，编辑
     * @param mActivity
     * @param map
     */
    public void saveOrUpdate(Context mActivity, Map<String, Object> map) {
        String aaa = map.toString();
        Log.e("李豆",aaa);
        DialogHelper.showProgressDialog(mActivity, null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
        APIManager.getInstance().getWebsiteInfoAPI().getSpecialQualificationAddOrUpdate(map).enqueue(new ICallBack<BaseEntity<UpdataSpecialQualificationEntity>>() {
            @Override
            public void onSuccess(BaseEntity<UpdataSpecialQualificationEntity> data) {
                if (data.getState().equals(APIManager.RESPONSE_OK)) {
                    getMessage().postValue("操作成功");
                }
            }
        });
    }

    /**
     * 专项资质类型
     * @param mActivity
     */
    public void getQualifyTypeTreeList(Context mActivity) {
        DialogHelper.showProgressDialog(mActivity, null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
        APIManager.getInstance().getWebsiteInfoAPI().getQualifyTypeTreeList().enqueue(new ICallBack<BaseEntity<List<PickSelectEntity<List<PickSelectEntity>>>>>() {
            @Override
            public void onSuccess(BaseEntity<List<PickSelectEntity<List<PickSelectEntity>>>> data) {
                if (data.getState().equals(APIManager.RESPONSE_OK)) {
                    typePickSelectList.postValue(data.getData());
                }
            }
        });
    }
    /**
     * 专项资质行业类型
     * @param mActivity
     */
    public void industryList(Context mActivity) {
        DialogHelper.showProgressDialog(mActivity, null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
        APIManager.getInstance().getWebsiteInfoAPI().industryList().enqueue(new ICallBack<BaseEntity<List<PickSelectEntity<List<PickSelectEntity>>>>>() {
            @Override
            public void onSuccess(BaseEntity<List<PickSelectEntity<List<PickSelectEntity>>>> data) {
                if (data.getState().equals(APIManager.RESPONSE_OK)) {
                    typehangyeSelectList.postValue(data.getData());
                }
            }
        });
    }

    /**
     * 专项资质 备案单位
     * @param mActivity
     */
    public void findProvinceRecordUnit(Context mActivity) {
        DialogHelper.showProgressDialog(mActivity, null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
        APIManager.getInstance().getWebsiteInfoAPI().findProvinceRecordUnit().enqueue(new ICallBack<BaseEntity<List<PickSelectEntity<List<PickSelectEntity>>>>>() {
            @Override
            public void onSuccess(BaseEntity<List<PickSelectEntity<List<PickSelectEntity>>>> data) {
                if (data.getState().equals(APIManager.RESPONSE_OK)) {
                    findProvinceRecordUnit.postValue(data.getData());
                }
            }
        });
    }

    /*
     * 商标权限 本地写入数据
     */
    public void getSBOption(Context mActivity) {
        DialogHelper.showProgressDialog(mActivity, null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
        PickSelectEntity entity1 = new PickSelectEntity("1", "所有人可见");
        PickSelectEntity entity2 = new PickSelectEntity("2", "仅会员可见");
        PickSelectEntity entity3 = new PickSelectEntity("3", "仅自己可见");
        List<PickSelectEntity> onlyDatas = new ArrayList<>();
        onlyDatas.add(entity1);
        onlyDatas.add(entity2);
        onlyDatas.add(entity3);
        DialogHelper.dismissProgressDialog();
        permissionPickSelectList.postValue(onlyDatas);
    }

    public void uploadAvatar(Context mContext, File file) {
        DialogHelper.showProgressDialog(mContext, null, "图片上传中...", 0, false, null).setCanceledOnTouchOutside(true);
        //创建map集合
        Map<String, RequestBody> photos = new HashMap<>();
        //将文件转换成多媒体文件，方便上传
        RequestBody requestBody = RequestBody.create(MediaType.parse("multipart/form-data"), file);
        photos.put("file" + "\"; filename=\"" + RegexUtils.getReplaceAll(file.getName(), "[\u4e00-\u9fa5]", "") + "image.jpg\"", requestBody);
        //接口请求
        APIManager.getInstance().getUploadAPI().uploadFile(photos).enqueue(new ICallBack<BaseEntity>() {
            @Override
            public void onSuccess(BaseEntity data) {
                if (data == null || data.getData() == null) {
                    MToast.showToast(mContext, "上传失败");
                    return;
                }
                urlString.postValue(data.getData().toString());
            }
        });
    }
}
